(function (exports) {

    var $ = null;
    var isMyTurn = true;

    var FightLayer = cc.Layer.extend({

        onEnter: function () {
            cc.Layer.prototype.onEnter.call(this);
        },
        onCCSLoadFinish: function () {
            var that = this;
        },
        onExit: function () {
            cc.Layer.prototype.onExit.call(this);
        },
        ctor: function () {
            this._super();

            var that = this;

            loadCCSTo(res.FightLayer_json, this, "Layer");
            $ = create$(this.getChildByName("Layer"));
            this.shipei();

            TouchUtils.setOnclickListener($('top.close'), function (node) {
                cc.director.popScene();
            });


            //atk
            TouchUtils.setOnclickListener($('top.btn_atk'), function (node) {
                that.myHero.atk('atk', that.yourHero);
            });
            TouchUtils.setOnclickListener($('top.btn_atk0'), function (node) {
                that.myHero.atk0('atk0', that.yourHero);
            });
            TouchUtils.setOnclickListener($('top.btn_atk1'), function (node) {
                that.myHero.atk1('atk1', that.yourHero);
            });
            TouchUtils.setOnclickListener($('top.btn_atk2'), function (node) {
                that.myHero.atk2('atk2', that.yourHero);
            });

            this.initHero();
            //this.alertQuestion();
        },
        shipei: function(){
            if(isIpad()){
                $('top').setPositionY(cc.winSize.height);
                for(var i=0;i<=2;i++){
                    $('root.bg_' + i).setPositionX(i*1024*1.25);
                    $('root.bg_' + i).setScale(1.25);
                }
            }
        },
        alertQuestion: function(){
            this.addChild(new QuestionLayer(10));
        },
        initHero: function(){
            if(!cc.sys.isNative)  return;
            //atk  atk0 atk1 atk2 atkd death idle idle2 run vertigo win win2
            var that = this;


            var myHero = new Hero008({
                'name': '008',
                'blood': 500,
                'blue': 0
            });
            myHero.setPosition(cc.p(-100, 200));
            $('root').addChild(myHero);
            this.myHero = myHero;

            myHero.playAnimation(0, 'run', true);
            myHero.runAction(cc.sequence(
                cc.moveBy(1.5, cc.p(500, 0)),
                cc.callFunc(function(){
                    that.fight();
                })
            ));


            var yourHero = new Hero004({
                'name': '004',
                'blood': 500,
                'blue': 0,
                fanzhuan: true
            });
            yourHero.setPosition(cc.p(cc.winSize.width + 100, 200));
            $('root').addChild(yourHero);
            this.yourHero = yourHero;

            yourHero.playAnimation(0, 'run', true);
            yourHero.runAction(cc.moveBy(1.5, cc.p(-500, 0)));
        },

        fight: function(){
            var that = this;

            if(this.yourHero.getBlood() <= 0){
                this.myHero.playAnimation(0, 'win', true);
                this.yourHero.playAnimation(0, 'death', false);

                var win = new sp.SkeletonAnimation('res/image/effect/battle_win/shengli.json',
                    'res/image/effect/battle_win/shengli.atlas');
                win.setPosition(cc.p(1024/2, 300));
                $('root').addChild(win, 10);
                win.addAnimation(0, '4_4', true);

                return;
            }
            if(this.myHero.getBlood() <= 0){
                this.myHero.playAnimation(0, 'death', false);
                this.yourHero.playAnimation(0, 'win', true);
                return;
            }
            if(isMyTurn) {
                if(this.myHero.getBlue() >= 100){
                    this.myHero.atk0('atk0', this.yourHero, function () {
                        that.fight();
                    });
                }else {
                    this.myHero.atk('atk', this.yourHero, function () {
                        that.fight();
                    });
                }
                isMyTurn = false;
            }else{
                if(this.yourHero.getBlue() >= 100) {
                    this.yourHero.atk0('atk0', this.myHero, function () {
                        that.fight();
                    });
                }else {
                    this.yourHero.atk('atk', this.myHero, function () {
                        that.fight();
                    });
                }
                isMyTurn = true;
            }
        },
    });
    exports.FightLayer = FightLayer;
})(window);
